﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class xrIrsaliyeOrtak : DevExpress.XtraReports.UI.XtraReport
    {
        public xrIrsaliyeOrtak()
        {
            InitializeComponent();
        }

        public xrIrsaliyeOrtak(int cikisEmriId)
            : this()
        {
            myData1.Merge(Session.DataManager.ExecuteDataSet("GetIrsaliyeOrtak", cikisEmriId));
            myData1.Irsaliye.Merge(myData1.Tables["Table"]);
            myData1.IrsaliyeLines.Merge(myData1.Tables["Table1"]);

        }


        private void xrTable10_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            int a = 0;
            xrTable10.HeightF = 127F;
            Detail.HeightF = 127F;
            if (myData1.IrsaliyeLinesDetail.Rows.Count > 0)
                a = myData1.IrsaliyeLinesDetail.Select("ProductId=" + GetCurrentColumnValue("ProductId").ToString()).Length;
            if (a > 2)
            {
                xrTable10.HeightF = a * 63.5F;
            }
        }

        private void xrLabel4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (myData1.IrsaliyeLinesDetail.Rows.Count > 0)
            {
                decimal metraj = Convert.ToDecimal(myData1.IrsaliyeLinesDetail.Compute("Sum(qty)", ""));
                xrLabel4.Text = metraj.ToString();
            }
        }

        private void xrPictureBox2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrPictureBox2.WidthF = xrTableCell19.WidthF;
        }

        private void xrTableCell20_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 12f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize, FontStyle.Bold);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize, FontStyle.Bold);
            gr.Dispose();
        }
    }
}
